package com.mall.biz.trade.impl.o2o.incluce;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.biz.trade.O2OTradeBiz;
import com.mall.biz.user.record.MinsuRecordBizImpl;
import com.mall.client.order.MinsuOrderService;
import com.mall.common.util.DateUtil;
import com.mall.dto.TradePriceAndBuyCountDTO;
import com.mall.pojo.MinsuOrder;
import com.mall.pojo.Trade;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.MoneyUtil;

@Service
public class MinSuTradeImpl implements O2OTradeBiz {
	private static final Logger log = LoggerFactory.getLogger(MinSuTradeImpl.class);
	@Autowired
	private MinsuOrderService minsuOrderService;
	@Autowired
	private MinsuRecordBizImpl minsuRecordBiz;
	@Override
	public TradePriceAndBuyCountDTO pack4PirceAndBuyCount(HttpServletRequest request, Integer type, Long price,
			Integer buyCount, Long parentId) {
		TradePriceAndBuyCountDTO tradePriceAndBuyCountDTO = new TradePriceAndBuyCountDTO();
		MinsuOrder minsuOrder = this.minsuOrderService.getMinsuOrderById(parentId);
		if(minsuOrder ==null){
			return null; // TODO 跳转到error页
		}
		tradePriceAndBuyCountDTO.setPrice(minsuOrder.getPrice());
		tradePriceAndBuyCountDTO.setBuyCount(minsuOrder.getCheckinDays());
		tradePriceAndBuyCountDTO.setDiscountPrice(0l);
		return tradePriceAndBuyCountDTO;
	}


	@Override
	public Boolean beforeInsert(HttpServletRequest request,Trade trade) {
		Long minsuId = HttpUtil.getLongValueFromRequest(request, "minsuId");
		Double price = Double.parseDouble(request.getParameter("money"));
		Long money = MoneyUtil.getDBMoney(price);
		Integer checkinPersons =HttpUtil.getIntegerValueFromRequest(request, "checkinPersons");
		String startTime = HttpUtil.getStringValueFromRequest(request, "startTime");
		String endTime = HttpUtil.getStringValueFromRequest(request, "endTime");
		Date startTimeDate = DateUtil.stringToDate(startTime + " " + "12:00:00", "yyyy-MM-dd HH:mm:ss");
		Date endTimeDate = DateUtil.stringToDate(endTime + " " + "12:00:00", "yyyy-MM-dd HH:mm:ss");
		
		
		Integer days = DateUtil.compareDate(startTime,endTime, 0);
		MinsuOrder minsuOrder = new MinsuOrder();
		minsuOrder.setMallId(trade.getMallId());
		minsuOrder.setMinsuId(minsuId);
		minsuOrder.setBuyUserId(trade.getBuyUserId());
		minsuOrder.setSellerId(trade.getSellerId());
		minsuOrder.setCheckinDays(days);
		minsuOrder.setCheckinPersons(checkinPersons);
		minsuOrder.setReservationStartTime(startTimeDate);
		minsuOrder.setReservationEndTime(endTimeDate);
		minsuOrder.setCommOnlyKey(trade.getCommOnlyKey());
		minsuOrder.setProvinceId(trade.getProvinceId());
		minsuOrder.setCityId(trade.getCityId());
		minsuOrder.setAreaId(trade.getAreaId());
		minsuOrder.setStatus(0);
		minsuOrder.setPrice(money);
		minsuOrder.setGmtCreate(new Date());
		minsuOrder.setGmtModify(new Date());
		this.minsuOrderService.insertMinsuOrder(minsuOrder);
		Long minsuOrderId = minsuOrder.getId();
		if (minsuOrderId == null) {
			log.error("民宿订单生成失败");
			return false;
		}
		trade.setParentId(minsuOrder.getId());
		return true;
	}

	@Override
	public Boolean afterSuccInsert(HttpServletRequest request, Trade trade) {
		minsuRecordBiz.payMinsuSuccessCallback(trade.getParentId());// 预定成给卖家发短信提醒
		return true;
	}

	@Override
	public Boolean afterErrorInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

}
